SED = sed
IPTABLES_BIN = iptables

ifndef $(IPTABLES_SRC)
IPTVER = \
	$(shell $(IPTABLES_BIN) --version | $(SED) -e 's/^iptables v//')
endif

ifneq ($(IPTVER),)
	IPTABLES_VERSION = $(IPTVER)
else
	IPTABLES_VERSION = $(shell cat $(IPTABLES_SRC)/Makefile | grep -e '^IPTABLES_VERSION:=' | cut -d"=" -f2)
endif

IPTABLES_OPTION = -DIPTABLES_VERSION=\"$(IPTABLES_VERSION)\"

obj-m += ipt_ipcontext.o
ipt_ipcontext-objs := ipcontext.o regsub.o parser.o log_trace.o

all: modules libipt_ipcontext.so

modules:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

libipt_ipcontext.so: libipt_ipcontext.c
	$(CC) $(CFLAGS) $(IPTABLES_OPTION) -fPIC -c libipt_ipcontext.c parseArgs.c 
	ld -shared -o libipt_ipcontext.so libipt_ipcontext.o parseArgs.o

clean:
	-rm -f *.o *.so *.ko .*.cmd *.mod.c
	-rm -rf .tmp_versions
